package com.ewili.framework.exception;

import com.ewili.framework.param.Response;
import com.ewili.framework.utils.GlobalConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    public static final String DEFAULT_ERROR_VIEW = "/404";
    private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 最外层的异常，默认异常
     *
     * @param req
     * @param e
     * @return
     * @throws Exception
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Object defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        logger.error("---DefaultException Handler---Host {} invokes url {} ERROR: {}", req.getRemoteHost(), req.getRequestURL(), e);
        Response response = new Response();
        response.setStatus(GlobalConstant.ResponseStatus.ERROR);
        response.setMsg(e.getMessage());
        return response;
    }

}
